<?php
class Gallery extends Controller {
    private $imgdata = array(
        'dir' => array(
        'original' => 'images/uploads/original/',
        'thumb' => 'images/uploads/thumb/',
        'large'=>'images/uploads/large/'
    ),
    'total' => 0,
    'images' => array(),
    'error' => ''
    );   
    
    public function addimage(){        
        $del_image_status= $this->uri->segment(5);
        $del_image= $this->uri->segment(4);
        if($del_image){
            $data['image_deleted']="Image {$del_image} deleted {$del_image_status}";
        }
        $this->load->model("gallery_model");
        /*if(!empty($offer_id)){            
            $offerdetails=  $this->offer_model->get_offer_details_by_id($offer_id);
            if($offerdetails){
                foreach ($partnerlang as $langkey => $langval) {
                    $tmptitle=  $this->offer_model->get_offer_details_lang_by_id($offerdetails[0]['title'], $langkey);
                    $tmpdesc=  $this->offer_model->get_offer_details_lang_by_id($offerdetails[0]['desc'], $langkey);
                    $offerdetails[0]["title".$langkey]=$tmptitle[0]['lvalue'];
                    $offerdetails[0]["desc".$langkey]=$tmpdesc[0]['lvalue'];
                }
                /*echo "<pre>";
                print_r($offerdetails);
                echo "</pre>";* /
                $data['offerdetails']=$offerdetails[0];
            }            
        }*/
        $gallery_arr = $this->gallery_model->get_gallery_list();
        $data['gallery_arr']=$gallery_arr;
        //print_r($hotel_arr);
        //$data['offer_type_arr']=array(array("offer_type_id"=>"BH", "offer_type_name"=>"Beach Holidays"), array("offer_type_id"=>"CH", "offer_type_name"=>"Culture & Heritage Tours"), array("offer_type_id"=>"WN", "offer_type_name"=>"Wildlife & Nature Tours"), array("offer_type_id"=>"RH", "offer_type_name"=>"Romantic Holidays"));
        $banner_arr = $this->gallery_model->get_gallery_list(1);
        $data['banner_arr']=$banner_arr;
        //$data['partnerlang']=$partnerlang;
        
        $this->load->library('breadcrumb');
        $this->breadcrumb->seperator='<div class="breadcrumb_divider"></div>';
        $this->breadcrumb->addCrumb('Gallery',base_url()."secu/gallery/addimage");
        $this->breadcrumb->addCrumb('Add/View Images',"#");
        $data['breadcrumb'] = $this->breadcrumb->makeBread();          
        
        $this->template->set('controller', $this);
        $this->template->set('title', 'gallery');
        $this->template->load_partial('admintemplate', 'gallery/manageimages', $data);
    }
    
    public function saveimages()
     {
        $upload_from= $this->uri->segment(4);
        //
        //$id=$this->input->post("id");
        $c_upload['upload_path']    = $this->imgdata['dir']['original'];
        $c_upload['allowed_types']  = 'gif|jpg|png|jpeg|x-png';
        $c_upload['max_size']       = '100000000';
        $c_upload['max_width']      = '10000';
        $c_upload['max_height']     = '5000';
        $c_upload['remove_spaces']  = TRUE;
        
        $form_fields=$_POST;
        //print_r($form_fields);
        //exit();
                
        $this->load->library('upload', $c_upload);

        if ($this->upload->do_upload()) {
//
            $img = $this->upload->data();
            $new_image = $this->imgdata['dir']['original'].trim(time())."_".$img['file_name'];
            $this->img_resize($img, $new_image,$id);
           
            unlink($this->imgdata['dir']['original'].$img['file_name']);
            //exit($this->imgdata['dir']['original'].$img['file_name']);
            $this->load->model('content');
            //exit();
            $this->content->saveimagestodb($id,trim(time())."_".$img['file_name'],$upload_from,$form_fields);

//exit($upload_from); 

        } else {
            $this->imgerror= $this->upload->display_errors();            
        }        
        redirect('secu/gallery/addimage');
        

    }
    
    public function img_resize($upload,$new_image,$id)
    {
        $this->load->library('image_lib');
        $newpath = $new_image;

        $config['image_library'] = 'gd2';
        $config['source_image']    = $upload['full_path'];
        $config['new_image'] = $newpath;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 595;
        $config['height']    = 320;

        $this->image_lib->initialize($config);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }

        $newpath_thumb = $this->imgdata['dir']['thumb']."thumb_".trim(time())."_".$upload['file_name'];

        unset($config);
        $this->image_lib->clear();
         $this->make_thumb($upload['full_path'],$newpath_thumb);
    }
    
    public function make_thumb($tempimage,$path)
    {

        $config['image_library'] = 'gd2';
        $config['source_image']    = $tempimage;
        $config['new_image'] = $path;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 100;
        $config['height']    = 80;

        $this->image_lib->initialize($config);

        if ( ! $this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        }
    }
    
    public function deletegalleryimage()
    {
        $original_status=false;
        $thumb_status=false;
        $del_image_status="failed";
        $id=$this->uri->segment(7);
        $ori_img=$this->uri->segment(5); 
        $ext=  substr($ori_img, -3);        
        $ori_img=  substr($ori_img, 0, strlen($ori_img)-4).'.'.$ext;
        $this->load->model('content');
        $this->content->deleteimage($id, "galleryimg");
        //
        //echo $this->imgdata['dir']['original'].$ori_img.'<br>';
        if (file_exists($this->imgdata['dir']['original'].$ori_img)){
            unlink($this->imgdata['dir']['original'].$ori_img);
            $original_status=true;
        }
        //echo $this->imgdata['dir']['thumb'].'thumb_'.$ori_img;
        if (file_exists($this->imgdata['dir']['thumb'].'thumb_'.$ori_img)){
            unlink($this->imgdata['dir']['thumb'].'thumb_'.$ori_img);
            $thumb_status=true;
        }
        //exit();
        if ($original_status && $thumb_status){
            $del_image_status="successfully";
        }elseif ($original_status || $thumb_status) {
            $del_image_status="partially";
        }
        redirect('secu/gallery/addimage/'.$ori_img.'/'.$del_image_status);
    }
}
?>